<?php
/**
 *
 */
class Sx_friend extends CI_Controller {
	/*执行操作(add,edit,del,show,showall)*/
	private $postAction;
	/*手机KEY*/
	private $postMobiKey;
	/*sessionID*/
	private $postSessionId;
	/*用户ID*/
	private $userID;
	/*好友ID*/
	private $friendID;
	/*用户信息数组(包括用户ID，用户name)*/
	private $userArr;
	/*用户email*/
	private $userEmail;
	/*用户介绍*/
	private $userIntr;
	/*用户头像*/
	private $userImg;
	/*用户城市*/
	private $userCity;
	/*用户昵称*/
	private $userNicename;
	/*用户url*/
	private $userUrl;
	/*用户编辑资料数组*/
	private $editArr;
	/*搜索好友条件*/
	private $condition = array("user_login", "user_city", "user_nicename");
	/*搜索好友值*/
	private $conditioncode;
	
	private $cursor;
	private $offset;

	public function index() {
		/*如果直接访问此页面acction*/
		$post = $this -> input -> post('action');
		if (!$post || empty($post)) {
			print($this -> fengchaoclass -> showhckmsg("11", NULL, $this -> lang -> line('hck_no'), TRUE));
			exit ;
		} else {
			$this -> postAction = $this -> input -> post('action');
		}
		/*判断用户权限*/
		$this -> postMobiKey = $this -> input -> post('mobikey');
		$this -> postSessionId = $this -> input -> post('sessionid');
		$this -> userArr = $this -> sx_useropen_mod -> user_con($this -> postMobiKey, $this -> postSessionId, TRUE);
		if (!$this -> userArr) {
			/*session或者Key过期，验证失败*/
			print($this -> fengchaoclass -> showhckmsg("-1", NULL, $this -> lang -> line('hck_friend_out'), TRUE));
			exit ;
		} else {
			/*执行用户好友添加删除修改操作*/
			/*载入sx_friend_mod model*/
			$this -> userID = $this -> userArr['userid'];
			$this -> load -> model("sx_friend_mod");
			switch ($this->postAction) {
				case 'add' :
				/*添加好友*/
					$this -> addFriend($this -> input -> post('fid'));
					break;
				/*显示用户基本信息*/
				case 'show' :
					$this -> showFriend($this -> input -> post('fid'));
					break;
				/*删除好友*/
				case 'del' :
					$this -> delFriend($this -> input -> post('fid'));
					break;
				/*修改用户资料*/
				case 'edit' :
					$this -> editUser();
					break;
				case 'showfriend' :
					$this->cursor = $this->input->post("cursor");
					$this->offset = $this->input->post("offset");
					$this -> showAllFriend($this -> userID,$this->cursor,$this->offset);
					break;
				/*好友搜索*/
				case 'search' :
				//值，条件
					$this -> searchFriend($this -> input -> post('condition'), $this -> input -> post("conditioncode"));
					break;
				default :
					print($this -> fengchaoclass -> showhckmsg("3", NULL, $this -> lang -> line('hck_friend_actionerr'), TRUE));
					break;
			}
		}
	}

	// ----------------------------------------------------------------
	/*
	 * 添加好友 addFriend
	 * @access	private
	 * @param	$fid  好友ID 	string
	 * @return	json/array
	 */
	// ----------------------------------------------------------------
	private function addFriend($fid) {
		if ($fid == NULL) {
			/*如果好友ID为空*/
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_friend_idnull'), TRUE));
			exit ;
		} else {
			/*取得好友ID*/
			$this -> friendID = $fid;
		}
		/*取得用户ID*/
		$this -> userID = $this -> userArr['userid'];
		/*不能添加自己*/
		if ($this -> friendID == $this -> userID) {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_friend_addself'), TRUE));
			exit ;
		}
		/*判断好友是否存在*/
		if ($this -> sx_friend_mod -> findFriend($this -> userID, $this -> friendID)) {
			/*好友存在则不添加，并提示客户端*/
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_friend_add_exist'), TRUE));
			exit ;
		}
		/*好友不存在，添加好友*/
		if ($this -> sx_friend_mod -> addFriend($this -> userID, $this -> friendID)) {
			/*好友添加成功*/
			print($this -> fengchaoclass -> showhckmsg("0", NULL, $this -> lang -> line('hck_friend_add_success'), TRUE));
		} else {
			/*添加好友失败*/
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_friend_add_error'), TRUE));

		}
	}

	// ----------------------------------------------------------------
	/*
	 * 查看好友信息
	 */
	// ----------------------------------------------------------------
	private function showFriend($id = "") {
		/*如果传入ID号为空，返回错误信息*/
		if (empty($id)) {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_friend_idnull'), TRUE));
			exit ;

		}
		/*组合JSON数据*/
		$this -> userID = $this -> userArr['userid'];
		$this -> friendID = $id;
		$addT = $this -> sx_friend_mod -> findFriend($this -> userID, $this -> friendID);
		$return_friend = $this -> sx_friend_mod -> selectFriend($id);
		if ($addT) {
			/*如果存在好友列表，则返回添加好友时间*/
			$return_friend['addtime'] = date("Y-m-d H:i:s", $addT[$this -> friendID]['addtime']);
		}

		//$return_friend = $this->sx_useropen_mod->userall($id, "","");
		if ($return_friend) {
			/*向客户端输出JSON数据*/
			print(json_encode($return_friend));
		} else {
			/*如有错误，输出错误*/
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_friend_showerr'), TRUE));
		}
	}

	// ----------------------------------------------------------------
	/*
	 * 编辑用户($id)
	 */
	// ----------------------------------------------------------------
	private function editUser() {

		/*取得用户ID，如果要修改的用户ID为空，则默认修改当前用户*/
		$this -> userID = $this -> input -> post("userId");
		$this -> userID == TRUE && !empty($this -> userID) ? $this -> userID : $this -> userID = $this -> userArr['userid'];
		/*要修改的字段和数据*/
		$this -> userNicename = $this -> input -> post("userNicename");
		$this -> userCity = $this -> input -> post("userCity");
		$this -> userEmail = $this -> input -> post("userEmail");
		/*调用用户上传文件接口*/
		$this -> userImg = $this -> input -> post("userImg");
		;
		$this -> userIntr = $this -> input -> post("userIntr");
		$this -> userUrl = $this -> input -> post("userUrl");
		/*修改字段数组*/
		$this -> editArr = array("user_nicename" => "$this->userNicename", "user_nicename" => "$this->userNicename", "user_email" => "$this->userEmail", "user_city" => "$this->userCity", "user_img" => "$this->userImg", "user_url" => "$this->userUrl", "user_intr" => "$this->userIntr");
		/*执行sql操作*/
		$editu = $this -> sx_useropen_mod -> userEdit($this -> editArr, $this -> userID);
		/*向客户端输出成功失败消息*/
		$editu == TRUE ? print($this -> fengchaoclass -> showhckmsg("0", NULL, $this -> lang -> line('hck_edituser_Y'), TRUE)) : print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_edituser_N'), TRUE));
	}

	// ----------------------------------------------------------------
	/*
	 * 删除好友
	 */
	// ----------------------------------------------------------------
	private function delFriend($fid) {
		//取得好友ID
		if ($fid == TRUE || !empty($fid)) {
			$this -> friendID = $fid;
		} else {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_friend_idnull'), TRUE));
			exit ;
		}
		//取得当前用户ID
		$this -> userID = $this -> userArr['userid'];
		$R = $this -> sx_friend_mod -> del_friend($this -> userID, $this -> friendID);
		if ($R) {
			switch ($R) {
				case '1':
				/*删除好友成功*/
					print($this -> fengchaoclass -> showhckmsg("0", NULL, $this -> lang -> line('hck_friend_del_Y'), TRUE));
					break;
				case '-1':
					/*删除好友失败*/
					print($this -> fengchaoclass -> showhckmsg("2", NULL, $this -> lang -> line('hck_friend_del_N'), TRUE));
					break;
				default:
					/*删除好友异常*/
					print($this -> fengchaoclass -> showhckmsg("3", NULL, $this -> lang -> line('hck_friend_del_err'), TRUE));
					break;
			}
		} else {
			/*传入好友ID不存在*/
			print($this -> fengchaoclass -> showhckmsg("4", NULL, $this -> lang -> line('hck_friend_null'), TRUE));
		}
	}

	// ----------------------------------------------------------------
	/*
	 * 显示用户的所有好友
	 */
	// ----------------------------------------------------------------
	private function showAllFriend($uid="",$cursor="",$offset="") {
		isset($uid) && !empty($uid) ? $uid : $this -> userID = $this -> userArr['userid'];
		empty($cursor)?$cursor=0:$cursor;
		empty($offset)?$offset=5:$cursor;
		$all = array($offset,$cursor);		
		$addT = $this -> sx_friend_mod -> findFriend($this -> userID, "",$all);
		$rArr = array('Msg_error' => "0", 'Msg' => "", 'MsgTime' => date("Y-m-d H:i:s"));		
		if($addT ==FALSE){
			print($this -> fengchaoclass -> showhckmsg("4", NULL, $this -> lang -> line('hck_friend_showmyf'), TRUE));
		}else{
			//返回总条数
			$rArr['total'] = $this -> sx_friend_mod -> fc01($this -> userID);
			$rArr['Resource'] = $addT;
			print(json_encode($rArr));
		}

	}

	// ----------------------------------------------------------------
	/**
	 * 搜索好友
	 * $n 条件
	 * $m 值
	 */
	// ----------------------------------------------------------------
	private function searchFriend($m = NULL, $n = FALSE) {
		/*判断搜索条件是什么，如：姓名，昵称，城市*/
		$this -> conditioncode = $m;
		empty($this -> conditioncode) ? $this -> conditioncode = 0 : $this -> conditioncode;
		$rArr = array('Msg_error' => "0", 'Msg' => "", 'MsgTime' => date("Y-m-d H:i:s"));
		$zyl = $this -> sx_friend_mod -> searchFriend($this -> condition[$n], $this -> conditioncode);
		if ($this -> sx_friend_mod -> searchFriend($this -> condition[$n], $this -> conditioncode) == FALSE) {
			//没有找到用户
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_friend_null'), TRUE));
		} else {
			//找到了
			$rArr['Resource'] = $zyl;
			print(json_encode($rArr));
		}

	}

}
?>